﻿Imports System.Data
Imports System.Data.SqlClient

Namespace DB

    ''' <summary>
    ''' TypeName:    InventoryBatchType
    ''' Author:      GeneratedCustomTemplate
    ''' DBTableName: STK_InventoryBatch
    ''' </summary>
    ''' <remarks></remarks>

    Public Class InventoryBatchType
        Inherits DBTableObjectBase

#Region "Declarations"
        'Protected Friend iInventoryBatchId as Guid
        Protected Friend iBatchType As String = "GRN Entry"
        Protected Friend iGrnReferenceNo As String
        Protected Friend iCommonReferenceNumber As String
        Protected Friend iPurchaseOrderNumber As String
        Protected Friend iFileName As String
        Protected Friend iFileChecksum As Long
        Protected Friend iDeliveryDateLoadedDate As DateTime
        Protected Friend iRemarks As String
        Protected Friend iAlteredByUser As String
#End Region

#Region "Constructors"
        Public Sub New()
            MyBase.New(TableNamesEnum.STK_InventoryBatch)
        End Sub
        Public Sub New(ByVal InventoryBatchId As Guid)
            MyBase.New(TableNamesEnum.STK_InventoryBatch, InventoryBatchId)
        End Sub
        Public Sub New(ByVal InventoryBatchId As Guid, _
                        ByVal BatchType As STK_InventoryMethodsEnum)
            MyBase.New(TableNamesEnum.STK_InventoryBatch)
            Me.iId = InventoryBatchId
            Me.iBatchType = BatchType.ToString.Replace("_", " ")
        End Sub
#End Region

#Region "Exposed Properties"
        Public Property InventoryBatchId() As Guid
            Get
                Return Me.iId
            End Get
            Set(ByVal value As Guid)
                Me.iId = value
            End Set
        End Property
        Public Property BatchType() As STK_InventoryMethodsEnum
            Get
                Return [Enum].Parse(GetType(STK_InventoryMethodsEnum), Me.iBatchType.Trim().Replace(" ", "_"))
            End Get
            Set(ByVal value As STK_InventoryMethodsEnum)
                Me.iBatchType = value.ToString.Replace("_", " ")
            End Set
        End Property
        Public Property GrnReferenceNo() As String
            Get
                Return Me.iGrnReferenceNo
            End Get
            Set(ByVal value As String)
                Me.iGrnReferenceNo = value
            End Set
        End Property
        Public Property CommonReferenceNumber() As String
            Get
                Return Me.iCommonReferenceNumber
            End Get
            Set(ByVal value As String)
                Me.iCommonReferenceNumber = value
            End Set
        End Property
        Public Property PurchaseOrderNumber() As String
            Get
                Return Me.iPurchaseOrderNumber
            End Get
            Set(ByVal value As String)
                Me.iPurchaseOrderNumber = value
            End Set
        End Property
        Public Property FileName() As String
            Get
                Return Me.iFileName
            End Get
            Set(ByVal value As String)
                Me.iFileName = value
            End Set
        End Property
        Public Property FileChecksum() As Long
            Get
                Return Me.iFileChecksum
            End Get
            Set(ByVal value As Long)
                Me.iFileChecksum = value
            End Set
        End Property
        Public Property DeliveryDateLoadedDate() As DateTime
            Get
                Return Me.iDeliveryDateLoadedDate
            End Get
            Set(ByVal value As DateTime)
                Me.iDeliveryDateLoadedDate = value
            End Set
        End Property
        Public Property Remarks() As String
            Get
                Return Me.iRemarks
            End Get
            Set(ByVal value As String)
                Me.iRemarks = value
            End Set
        End Property
        Public Property AlteredByUser() As String
            Get
                Return Me.iAlteredByUser
            End Get
            Set(ByVal value As String)
                Me.iAlteredByUser = value
            End Set
        End Property
#End Region

#Region "Implementations"
        Protected Friend Overloads Function Load(ByVal Row As DataRow) As Boolean
            If IsDBNull(Row(0)) Then Me.iId = Guid.Empty Else Me.iId = Row(0)
            If IsDBNull(Row(1)) Then Me.iBatchType = Nothing Else Me.iBatchType = Row(1)
            If IsDBNull(Row(2)) Then Me.iGrnReferenceNo = Nothing Else Me.iGrnReferenceNo = Row(2)
            If IsDBNull(Row(3)) Then Me.iCommonReferenceNumber = Nothing Else Me.iCommonReferenceNumber = Row(3)
            If IsDBNull(Row(4)) Then Me.iPurchaseOrderNumber = Nothing Else Me.iPurchaseOrderNumber = Row(4)
            If IsDBNull(Row(5)) Then Me.iFileName = Nothing Else Me.iFileName = Row(5)
            If IsDBNull(Row(6)) Then Me.iFileChecksum = Nothing Else Me.iFileChecksum = Row(6)
            If IsDBNull(Row(7)) Then Me.iDeliveryDateLoadedDate = Nothing Else Me.iDeliveryDateLoadedDate = Row(7)
            If IsDBNull(Row(8)) Then Me.iRemarks = Nothing Else Me.iRemarks = Row(8)
            If IsDBNull(Row(9)) Then Me.iAlteredByUser = Nothing Else Me.iAlteredByUser = Row(9)
            If IsDBNull(Row(10)) Then Me.iLMUserId = Nothing Else Me.iLMUserId = Row(10)
            If IsDBNull(Row(11)) Then Me.iLMDate = Nothing Else Me.iLMDate = Row(11)
            Return True
        End Function
        Protected Friend Overrides Function Load() As Boolean
            'sp_[TableName]Get
            Dim resBool As Boolean = True
            Dim da As New SqlDataAdapter
            Dim cmd As New SqlCommand("sp_" & Me.iTableName.ToString.Replace("COMP_", "") & "Get '" & Me.iId.ToString("D") & "'")
            cmd.Connection = Me.iConnection
            If IsNothing(Me.iTransaction) = False Then cmd.Transaction = Me.iTransaction

            Dim dt As DataTable = New DataTable
            dt = Utility.GetList(cmd)

            If dt.Rows.Count > 0 Then
                Me.Load(dt.Rows(0))
            Else
                resBool = False
            End If

            Return resBool
        End Function
        Protected Friend Overrides Function Insert() As Boolean
            'sp_[TableName]Insert
            Dim resBool As Boolean = True
            Dim resInt As Integer
            Dim da As New SqlDataAdapter

            Dim cmd As New SqlCommand("sp_" & Me.iTableName.ToString.Replace("COMP_", "") & "Insert")
            cmd.Connection = Me.iConnection
            If IsNothing(Me.iTransaction) = False Then cmd.Transaction = Me.iTransaction
            'Set command type to Stoed Procedure
            cmd.CommandType = CommandType.StoredProcedure
            'Validation or required parameters
            Try
                'Populate Stored Procedure parameters
                With cmd.Parameters
                    If (Me.iId = Guid.Empty) Then
                        .AddWithValue("@InventoryBatchId", System.DBNull.Value)
                    Else
                        .AddWithValue("@InventoryBatchId", Me.iId)
                    End If
                    If (Me.iBatchType = String.Empty) Then
                        .AddWithValue("@BatchType", System.DBNull.Value)
                    Else
                        .AddWithValue("@BatchType", Me.iBatchType)
                    End If
                    If (Me.iGrnReferenceNo = String.Empty) Then
                        .AddWithValue("@GrnReferenceNo", System.DBNull.Value)
                    Else
                        .AddWithValue("@GrnReferenceNo", Me.iGrnReferenceNo)
                    End If
                    If (Me.iCommonReferenceNumber = String.Empty) Then
                        .AddWithValue("@CommonReferenceNumber", System.DBNull.Value)
                    Else
                        .AddWithValue("@CommonReferenceNumber", Me.iCommonReferenceNumber)
                    End If
                    If (Me.iPurchaseOrderNumber = String.Empty) Then
                        .AddWithValue("@PurchaseOrderNumber", System.DBNull.Value)
                    Else
                        .AddWithValue("@PurchaseOrderNumber", Me.iPurchaseOrderNumber)
                    End If
                    If (Me.iFileName = String.Empty) Then
                        .AddWithValue("@FileName", System.DBNull.Value)
                    Else
                        .AddWithValue("@FileName", Me.iFileName)
                    End If
                    .AddWithValue("@FileChecksum", Me.iFileChecksum)
                    If (Me.iDeliveryDateLoadedDate = DateTime.MinValue) Then
                        .AddWithValue("@DeliveryDateLoadedDate", System.DBNull.Value)
                    Else
                        .AddWithValue("@DeliveryDateLoadedDate", Me.iDeliveryDateLoadedDate)
                    End If
                    If (Me.iRemarks = String.Empty) Then
                        .AddWithValue("@Remarks", System.DBNull.Value)
                    Else
                        .AddWithValue("@Remarks", Me.iRemarks)
                    End If
                    If (Me.iAlteredByUser = String.Empty) Then
                        .AddWithValue("@AlteredByUser", System.DBNull.Value)
                    Else
                        .AddWithValue("@AlteredByUser", Me.iAlteredByUser)
                    End If
                    If (Me.iLMUserId = Guid.Empty) Then
                        .AddWithValue("@LMUserId", System.DBNull.Value)
                    Else
                        .AddWithValue("@LMUserId", Me.iLMUserId)
                    End If
                    If (Me.iLMDate = DateTime.MinValue) Then
                        .AddWithValue("@LMDate", System.DBNull.Value)
                    Else
                        .AddWithValue("@LMDate", Me.iLMDate)
                    End If
                End With
                'Execute Stored Procedure
                resInt = cmd.ExecuteNonQuery()
            Catch ex As Exception
                resBool = False
                Throw ex
            End Try
            If resInt = 0 Then
                Throw New RequestedRecordDoesNotExistException("Requested changes failed to insert, no records were modified")
            End If
            Return resBool
        End Function
        Protected Friend Overrides Function Update() As Boolean
            'sp_[TableName]Update
            Dim resBool As Boolean = True
            Dim resInt As Integer
            Dim da As New SqlDataAdapter
            Dim cmd As New SqlCommand("sp_" & Me.iTableName.ToString.Replace("COMP_", "") & "Update")
            cmd.Connection = Me.iConnection
            If IsNothing(Me.iTransaction) = False Then cmd.Transaction = Me.iTransaction
            'Set command type to Stoed Procedure
            cmd.CommandType = CommandType.StoredProcedure
            'Validation or required parameters
            Try
                'Populate Stored Procedure parameters
                With cmd.Parameters
                    If (Me.iId = Guid.Empty) Then
                        .AddWithValue("@InventoryBatchId", System.DBNull.Value)
                    Else
                        .AddWithValue("@InventoryBatchId", Me.iId)
                    End If
                    If (Me.iBatchType = String.Empty) Then
                        .AddWithValue("@BatchType", System.DBNull.Value)
                    Else
                        .AddWithValue("@BatchType", Me.iBatchType)
                    End If
                    If (Me.iGrnReferenceNo = String.Empty) Then
                        .AddWithValue("@GrnReferenceNo", System.DBNull.Value)
                    Else
                        .AddWithValue("@GrnReferenceNo", Me.iGrnReferenceNo)
                    End If
                    If (Me.iCommonReferenceNumber = String.Empty) Then
                        .AddWithValue("@CommonReferenceNumber", System.DBNull.Value)
                    Else
                        .AddWithValue("@CommonReferenceNumber", Me.iCommonReferenceNumber)
                    End If
                    If (Me.iPurchaseOrderNumber = String.Empty) Then
                        .AddWithValue("@PurchaseOrderNumber", System.DBNull.Value)
                    Else
                        .AddWithValue("@PurchaseOrderNumber", Me.iPurchaseOrderNumber)
                    End If
                    If (Me.iFileName = String.Empty) Then
                        .AddWithValue("@FileName", System.DBNull.Value)
                    Else
                        .AddWithValue("@FileName", Me.iFileName)
                    End If
                    .AddWithValue("@FileChecksum", Me.iFileChecksum)
                    If (Me.iDeliveryDateLoadedDate = DateTime.MinValue) Then
                        .AddWithValue("@DeliveryDateLoadedDate", System.DBNull.Value)
                    Else
                        .AddWithValue("@DeliveryDateLoadedDate", Me.iDeliveryDateLoadedDate)
                    End If
                    If (Me.iRemarks = String.Empty) Then
                        .AddWithValue("@Remarks", System.DBNull.Value)
                    Else
                        .AddWithValue("@Remarks", Me.iRemarks)
                    End If
                    If (Me.iAlteredByUser = String.Empty) Then
                        .AddWithValue("@AlteredByUser", System.DBNull.Value)
                    Else
                        .AddWithValue("@AlteredByUser", Me.iAlteredByUser)
                    End If
                    If (Me.iLMUserId = Guid.Empty) Then
                        .AddWithValue("@LMUserId", System.DBNull.Value)
                    Else
                        .AddWithValue("@LMUserId", Me.iLMUserId)
                    End If
                    If (Me.iLMDate = DateTime.MinValue) Then
                        .AddWithValue("@LMDate", System.DBNull.Value)
                    Else
                        .AddWithValue("@LMDate", Me.iLMDate)
                    End If
                End With
                'Execute Stored Procedure
                resInt = cmd.ExecuteNonQuery()
            Catch ex As Exception
                resBool = False
                Throw ex
            End Try
            If resInt = 0 Then
                Throw New RequestedRecordDoesNotExistException("Requested changes failed to update, no records were modified")
            End If
            Return resBool
        End Function
        Protected Friend Overrides Function ValidateOnDelete() As Boolean
            Return True
        End Function
        Protected Friend Overrides Function ValidateOnInsert() As Boolean
            Return True
        End Function
        Protected Friend Overrides Function ValidateOnUpdate() As Boolean
            Return True
        End Function
#End Region

#Region "Overrides"
        Public Overrides Function ToString() As String
            Return Me.iId.ToString
        End Function
        Public Overrides Function ToDetailsListBoxItem() As WindowsExtendedControls.DetailListBoxItem
            Dim header As String = ""
            Dim details As String = ""
            Dim footer As String = ""
            'Header
            If Me.BatchType = STK_InventoryMethodsEnum.GRN_Entry Then
                header = Me.iGrnReferenceNo
            Else
                header = Me.iFileName.Substring(Me.iFileName.LastIndexOf("\") + 1, Me.iFileName.Length - Me.iFileName.LastIndexOf("\") - 1)
            End If
            'Details
            If (Me.iFileName <> "") AndAlso (Me.BatchType = STK_InventoryMethodsEnum.File_Import) Then
                If details <> "" Then
                    details &= vbNewLine & "FullPath: " & Me.iFileName
                Else
                    details &= "FullPath: " & Me.iFileName
                End If
            End If
            If Me.iCommonReferenceNumber <> "" Then
                If details <> "" Then
                    details &= vbNewLine & "Common Reference No: " & Me.iCommonReferenceNumber
                Else
                    details &= "Common Reference No: " & Me.iCommonReferenceNumber
                End If
            End If
            If Me.iPurchaseOrderNumber <> "" Then
                If details <> "" Then
                    details &= vbNewLine & "PO No: " & Me.iPurchaseOrderNumber
                Else
                    details &= "PO No: " & Me.iPurchaseOrderNumber
                End If
            End If
            If Me.iRemarks <> "" Then
                If details <> "" Then
                    details &= vbNewLine & Me.iRemarks
                Else
                    details &= Me.iRemarks
                End If
            End If
            'Footer
            If Me.BatchType = STK_InventoryMethodsEnum.GRN_Entry Then
                footer = "Delivery Date: " & Me.iDeliveryDateLoadedDate.ToString("dd-MMM-yy")
            Else
                footer = "Loaded Date: " & Me.iDeliveryDateLoadedDate.ToString("dd-MMM-yy")
            End If
            Return New WindowsExtendedControls.DetailListBoxItem(header, details, footer, Nothing, Me)
        End Function
#End Region

    End Class

    Public Class InventoryBatchTypeCollection
        Inherits DBTableObjectCollectionBase

        Public DataFilters As New DataFilterTypeCollection(Of InventoryBatchTypeFields)(Me.iTablename.ToString)
        Public DataSorters As New DataSorterTypeCollection(Of InventoryBatchTypeFields)(Me.iTableName.ToString)

        Public Sub New()
            MyBase.New(TableNamesEnum.STK_InventoryBatch)
        End Sub

        Default Public Property Item(ByVal Index As Integer) As InventoryBatchType
            Get
                Return CType(Me.List.Item(Index), InventoryBatchType)
            End Get
            Set(ByVal value As InventoryBatchType)
                Me.List.Item(Index) = value
            End Set
        End Property

        Protected Friend Overrides Function Load() As Boolean
            Return Me.Load("SELECT * FROM " & Me.iTableName.ToString & Me.DataFilters.GetSQL & Me.DataSorters.GetSQL)
        End Function

        Protected Friend Overloads Function Load(ByVal CustomFilter As String) As Boolean
            'Get data from DB
            Dim cmd As New SqlCommand
            cmd.Connection = Me.iConnection
            If IsNothing(Me.iTransaction) = False Then cmd.Transaction = Me.iTransaction
            Dim dt As DataTable
            Try
                cmd.CommandText = CustomFilter
                dt = Utility.GetList(cmd)
            Catch ex As Exception
                Throw ex
            End Try
            'Clear exising list
            Dim row As DataRow
            List.Clear()

            'Build new list
            Dim typeObject As InventoryBatchType

            For Each row In dt.Rows
                typeObject = New InventoryBatchType()
                typeObject.iConnection = Me.iConnection
                typeObject.iTransaction = Me.iTransaction
                If typeObject.Load(row) = True Then Me.List.Add(typeObject)
            Next
            Return True
        End Function

        Public Overloads Sub Sort(ByVal propertyToSort As InventoryBatchTypeFields, ByVal sortOrder As SortOrder)
            ipropertyToSort = propertyToSort.ToString
            isortOrder = sortOrder
            Me.InnerList.Sort(Me)
        End Sub

    End Class

#Region "Enumerations"       'Enumerations
    Public Enum InventoryBatchTypeFields
        InventoryBatchId
        BatchType
        GrnReferenceNo
        CommonReferenceNumber
        PurchaseOrderNumber
        FileName
        FileChecksum
        DeliveryDateLoadedDate
        Remarks
        AlteredByUser
        LMUserId
        LMDate

    End Enum
#End Region

End Namespace